const mysql = require('mysql2')
const { connect } = require('../routes/images')
const pool = mysql.createPool({
  host: "localhost",
  user: "root",
  password: "123456",
  database: "tea-culture",
  connectTimeout: "1000",
})

const sqlConnection = (sql, params) => {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      if (err) {
        console.log('err:', err)
        reject(err)
        return
      }
      connection.query(sql, params, (err, results, fields) => {
        if (err) {
          console.log('err', err)
          reject(err)
        }
        resolve(results)
        pool.releaseConnection(connection)
        // connection.release()
      })
    })
  })
}

module.exports = {
  sqlConnection: sqlConnection,
}